package view;

import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import resources.ResourceProvider;

import model.EmailImp;
import model.MeetingMail;
import model.Template;

/**
 * Meeting panel class will be displayed by JPanel
 *
 */
public class MeetingPanel extends JPanel
{
    /**
     * 
     */
    private static final long serialVersionUID = 2202878495055738147L;
    
    private JTextField toOrFromTextField;
    private JTextField subjectTextField;
    private JTextField locationTextField;
    private JTextArea messageTextField;
    
    private JComboBox startDay;
    private JComboBox startMonth;
    private JComboBox startYear;
    private JComboBox startHour;
    private JComboBox startMinute;
    
    private JComboBox endDay;
    private JComboBox endMonth;
    private JComboBox endYear;
    private JComboBox endHour;
    private JComboBox endMinute;
    
    private JLabel toOrFrom;
    private MailDisplayArea mailDisplayArea;
    private MeetingMail email;

    /**
     * Constructor
     * @param  mailDisplayArea the reference of the mailDisplayArea
     */
    public MeetingPanel(MailDisplayArea mailDisplayArea)
    {
        this.mailDisplayArea = mailDisplayArea;
        this.initialize();
    }

    /**
     * Constructor
     * @param  mailDisplayArea the reference of the mailDisplayArea
     * @param email the email
     * @param reply true if it is a the reply email or false
     */
    MeetingPanel(MailDisplayArea mailDisplayArea,EmailImp email, boolean reply)
    {
        this.mailDisplayArea = mailDisplayArea;
        this.email = (MeetingMail) email;
        
        this.initialize();
         
        if(reply || email.getPersistencePath().contains(ResourceProvider.DRAFT))
        {
            this.toOrFrom.setText("To");
            StringBuilder receivers = new StringBuilder("");
            List<String> receiversList = email.getReceiver();
            
            for(int n=0; n < receiversList.size(); n++ )
            {
                receivers.append(receiversList.get(n));
                if(n != (receiversList.size() -1))
                {
                    receivers.append(",");
                }
            }
            
            this.toOrFromTextField.setText(receivers.toString());  
        }
        else
        {
                      
          if(email.getPersistencePath().contains(ResourceProvider.SENT) || email.getPersistencePath().contains(ResourceProvider.OUTBOX))
          {
              this.toOrFrom.setText("To");
              StringBuilder receivers = new StringBuilder("");
              List<String> receiversList = email.getReceiver();
              
              for(int n=0; n < receiversList.size(); n++ )
              {
                  receivers.append(receiversList.get(n));
                  if(n != (receiversList.size() -1))
                  {
                      receivers.append(",");
                  }
              }
              
              this.toOrFromTextField.setText(receivers.toString());
          }
          else
          {
              this.toOrFrom.setText("From");
              this.toOrFromTextField.setText(email.getSender());
          } 
          
        }
        
        
        this.subjectTextField.setText(email.getSubject());
        this.messageTextField.setText(email.getMessage());
        this.locationTextField.setText(this.email.getMeetingPlace()); 
        
        
        String start = this.email.getStartTime();
        
        String[] split1 = start.split(" ");
        String[] startDate = split1[0].split("_");
        String[] startTime = split1[1].split(":");
        
        startDay.setSelectedItem(startDate[2]);
        startMonth.setSelectedItem(startDate[1]);
        startYear.setSelectedItem(startDate[0]);
        startHour.setSelectedItem(startTime[0]);
        startMinute.setSelectedItem(startTime[1]);
        
        
        
        String end = this.email.getEndTime();
        String[] split2 = end.split(" ");
        String[] endDate = split2[0].split("_");
        String[] endTime = split2[1].split(":");
        
        endDay.setSelectedItem(endDate[2]);
        endMonth.setSelectedItem(endDate[1]);
        endYear.setSelectedItem(endDate[0]);
        endHour.setSelectedItem(endTime[0]);
        endMinute.setSelectedItem(endTime[1]);
    }
    
    /**
     * initialization
     */
    private void initialize()
    {
        this.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.setLayout(null);
        
        toOrFrom = new JLabel("To");
        toOrFrom.setBounds(new Rectangle(32,24,64,30));
        toOrFrom.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(toOrFrom);
        
        JLabel subjectLable = new JLabel("Subject");
        subjectLable.setBounds(new Rectangle(32,64,64,30));
        subjectLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(subjectLable);
        
        JLabel locationLable = new JLabel("Location");
        locationLable.setBounds(new Rectangle(32,104,64,30));
        locationLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(locationLable);
        
        JLabel startLable = new JLabel("StartTime");
        startLable.setBounds(new Rectangle(32,144,64,30));
        startLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(startLable);
        
        JLabel endLable = new JLabel("EndTime");
        endLable.setBounds(new Rectangle(32,184,64,30));
        endLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(endLable);
        
        JLabel contentLable = new JLabel("Message");
        contentLable.setBounds(new Rectangle(32,224,64,30));
        contentLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(contentLable);
        
        toOrFromTextField = new JTextField();
        toOrFromTextField.setBounds(new Rectangle(135,24,400,30));
        this.add(toOrFromTextField);
        
        subjectTextField = new JTextField();
        subjectTextField.setBounds(new Rectangle(135,64,400,30));
        this.add(subjectTextField);
        
        locationTextField = new JTextField();
        locationTextField.setBounds(new Rectangle(135,104,400,30));
        this.add(locationTextField);
        

        
        String[] date ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25","26","27","28","29","30","31", "Start"};
        startDay = new JComboBox(date);
        startDay.setBounds(135, 154, 50, 20);
        startDay.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent e) 
            {
                String selection = (String)startDay.getSelectedItem();
            }
            
         });
         
        this.add(startDay);
        
        

        String[] month ={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
        startMonth = new JComboBox(month);
        startMonth.setBounds(195, 154, 60, 20);
        startMonth.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent e) 
            {
                String selection = (String)startMonth.getSelectedItem();
            }
            
         });
         
        this.add(startMonth);
        
        String[] yeardata ={"2013","2014","2015","2016","2017","2018","2019","2020","2021","2022","2023","2024","2025","2026","2027","2028","2029","2030"};
        startYear = new JComboBox(yeardata);
        startYear.setBounds(250, 154, 65, 20);
        startYear.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent e) 
            {
                String selection = (String)startYear.getSelectedItem();
            }
            
         });
         
        this.add(startYear);
        
        JLabel lblNewLabelDay = new JLabel("DD");
        lblNewLabelDay.setBounds(135, 134, 46, 14);
        this.add(lblNewLabelDay);
        
        JLabel lblMonth = new JLabel("MM");
        lblMonth.setBounds(200, 134, 46, 14);
        this.add(lblMonth);
        
        JLabel lblYear = new JLabel("YY");
        lblYear.setBounds(255, 134, 46, 14);
        this.add(lblYear);
        

        String[] hour ={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"};
        startHour = new JComboBox(hour);
        startHour.setBounds(315, 154, 47, 20);
        startHour.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent e) 
            {
                String selection = (String)startHour.getSelectedItem();
            }
            
         });
         
        this.add(startHour);
            

        
        String[] min ={"00","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25","26","27","28","29","30","31","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
        startMinute = new JComboBox(min);
        startMinute.setBounds(360, 154, 52, 20);
        startMinute.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent e) 
            {
                String selection = (String)startMinute.getSelectedItem();
            }
            
         });
         
        this.add(startMinute);
        
                
         JLabel lblNewLabelHour = new JLabel("HH");
         lblNewLabelHour.setBounds(320, 134, 46, 14);
         this.add(lblNewLabelHour);
            
         JLabel lblMin = new JLabel("MIN");
         lblMin.setBounds(365, 134, 46, 14);
         this.add(lblMin);
            
            
             String[] date1 ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25","26","27","28","29","30","31"};
                endDay = new JComboBox(date1);
                endDay.setBounds(135, 184, 50, 20);
                endDay.addActionListener(new ActionListener()
                 {
                    public void actionPerformed(ActionEvent e) 
                    {
                        String selection = (String)endDay.getSelectedItem();
                    }
                    
                 });
                 
                this.add(endDay);
            

                String[] month1 ={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
                endMonth = new JComboBox(month1);
                endMonth.setBounds(195, 184, 60, 20);
                endMonth.addActionListener(new ActionListener()
                 {
                    public void actionPerformed(ActionEvent e) 
                    {
                        String selection = (String)endMonth.getSelectedItem();
                    }
                    
                 });
                 
                this.add(endMonth);
                
                
            
                String[] year1 ={"2013","2014","2015","2016","2017","2018","2019","2020","2021","2022","2023","2024","2025","2026","2027","2028","2029","2030"};
                endYear = new JComboBox(year1);
                endYear.setBounds(250, 184, 65, 20);
                endYear.addActionListener(new ActionListener()
                 {
                    public void actionPerformed(ActionEvent e) 
                    {
                        String selection = (String)endYear.getSelectedItem();
                    }
                    
                 });
                 
                this.add(endYear);
                
            

            
                String[] hour1 ={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"};
                endHour = new JComboBox(hour1);
                endHour.setBounds(315, 184, 47, 20);
                endHour.addActionListener(new ActionListener()
                 {
                    public void actionPerformed(ActionEvent e) 
                    {
                        String selection = (String)endHour.getSelectedItem();
                    }
                    
                 });
                 
                this.add(endHour);  
                
                String[] min1 ={"00","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","22","23","24","25","26","27","28","29","30","31","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
                endMinute = new JComboBox(min1);
                endMinute.setBounds(360, 184, 52, 20);
                endMinute.addActionListener(new ActionListener()
                 {
                    public void actionPerformed(ActionEvent e) 
                    {
                        String selection = (String)endMinute.getSelectedItem();
                    }
                    
                 });
                 
                this.add(endMinute);
                
    
        messageTextField = new JTextArea();
        
        JScrollPane scroll = new JScrollPane (messageTextField, 
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setBounds(new Rectangle(135,224,400,320));    
        this.add(scroll);
        
    }

    /**
     * @return Return the current email in the view
     */
    public MeetingMail getEmail()
    {
        String[] receivers = toOrFromTextField.getText().trim().split(",");
        
        List<String> receiverList = Arrays.asList(receivers);
        String sender = MeetingPanel.this.mailDisplayArea.getEmailApplicationView().
                getController().getUser();
        
        String subject = subjectTextField.getText().trim();
        String message = messageTextField.getText().trim();
        
        String dateF = "";
        if((this.email != null) && this.email.getPersistencePath().contains(ResourceProvider.DRAFT))
        {
            dateF = email.getTime();
        }
        else
        {
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            dateF = dateFormat.format(date);
            dateF = ResourceProvider.converDataFormatToPersistenceFormat(dateF);
        }
        
            
      
        
        String startTime  = startYear.getSelectedItem() + "_" + startMonth.getSelectedItem() + "_" + 
                      startDay.getSelectedItem() +" " + startHour.getSelectedItem() + ":" + startMinute.getSelectedItem() + ":" + "00";
 
        String endTime  = endYear.getSelectedItem() + "_" + endMonth.getSelectedItem() + "_" + 
                endDay.getSelectedItem() +" " + endHour.getSelectedItem() + ":" + endMinute.getSelectedItem() + ":" + "00";

        String location = locationTextField.getText().trim(); 
        
        MeetingMail email = new MeetingMail.Builder()
                                        .setSender(sender)
                                        .setReceiver(receiverList)
                                        .setSubject(subject)
                                        .setMessage(message)
                                        .setTimeOfCreation(dateF)
                                        .setStartTime(startTime)
                                        .setEndTime(endTime)
                                        .setPlace(location)
                                        .build(); 
        if(this.email != null)
        {
            email.setMeetingID(this.email.getMeetingId());
        }
        else
        {
            email.setMeetingID(dateF); 
        }
            
        if((this.email != null)) // && this.email.getPersistencePath().contains(ResourceProvider.DRAFT))
        {
            email.setPersistencePath(this.email.getPersistencePath());
        }
                
        return email;
     }
    
    /**
     * load template in meeting panel 
     * @param tempL the template to load
     */
    public void loadTemplate(Template tempL)
    {
        List<String> receiverList = tempL.getTo();
        StringBuilder rec = new StringBuilder();
        for(int n=0;n<receiverList.size();n++)
        {
            rec.append(receiverList.get(0));
            if(n != (receiverList.size()-1))
            {
                rec.append(",");  
            }    
        }
        
        String subject = tempL.getSubject();
        String message = tempL.getMessage();
        toOrFromTextField.setText(receiverList.toString());
        subjectTextField.setText(subject);
        messageTextField.setText(message);
    }
      
}

